<?php /* 	These code was generated using phpCIGen v 0.1.a (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #songbee	mukhlisona@gmail.com
	#CV. Trust Solution, jl. Saronojiwo 19 Surabaya, http://www.ts.co.id

	+ Module  		: m_staff Model
	+ Description	: For record model process back-end
	+ Filename 		: c_m_staff.php
 	+ creator 		: 
 	+ Created on 06/Mar/2012 12:35:49

*/

class M_m_staff extends CI_Model{

		//constructor
		function M_m_staff() {
			parent::__construct();
		}

		//function for get list record
		function m_staff_list($filter,$start,$end){
			$sql = "SELECT staff_id,
							 staff_name,
							 staff_address,
							 staff_city,
							 staff_hp,
							 staff_email					  FROM 	 m_staff";

			// For simple search
			if ($filter<>""){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(staff_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(staff_name) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(staff_address) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(staff_city) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(staff_hp) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(staff_email) LIKE '%".addslashes(strtolower($filter))."%' )";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function m_staff_create($staff_name,
				 $staff_address,
				 $staff_city,
				 $staff_hp,
				 $staff_email){
			$data = array(
						 "staff_name"=>$staff_name,						 "staff_address"=>$staff_address,						 "staff_city"=>$staff_city,						 "staff_hp"=>$staff_hp,						 "staff_email"=>$staff_email					);
			$this->db->insert('m_staff', $data);
			if($this->db->affected_rows())
				return '1';
			else
				return '0';
		}

		//function for update record
		function m_staff_update($staff_id,
				 $staff_name,
				 $staff_address,
				 $staff_city,
				 $staff_hp,
				 $staff_email){
			$data = array(
						"staff_name"=>$staff_name,						"staff_address"=>$staff_address,						"staff_city"=>$staff_city,						"staff_hp"=>$staff_hp,						"staff_email"=>$staff_email					);

			$this->db->where('staff_id', $staff_id);
			$this->db->update('m_staff', $data);
			return '1';
		}

		//fcuntion for delete record
		function m_staff_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the m_staffs at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM m_staff WHERE staff_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM m_staff WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "staff_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function m_staff_search($staff_id,
				 $staff_name,
				 $staff_address,
				 $staff_city,
				 $staff_hp,
				 $staff_email,$start,$end){
			//full query
			$sql = "SELECT staff_id,
							 staff_name,
							 staff_address,
							 staff_city,
							 staff_hp,
							 staff_email					  FROM 	 m_staff";

			if($staff_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(staff_id) LIKE '%".addslashes(strtolower($staff_id))."%'";
			};
			if($staff_name!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(staff_name) LIKE '%".addslashes(strtolower($staff_name))."%'";
			};
			if($staff_address!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(staff_address) LIKE '%".addslashes(strtolower($staff_address))."%'";
			};
			if($staff_city!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(staff_city) LIKE '%".addslashes(strtolower($staff_city))."%'";
			};
			if($staff_hp!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(staff_hp) LIKE '%".addslashes(strtolower($staff_hp))."%'";
			};
			if($staff_email!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(staff_email) LIKE '%".addslashes(strtolower($staff_email))."%'";
			};

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function m_staff_print($staff_id,
				 $staff_name,
				 $staff_address,
				 $staff_city,
				 $staff_hp,
				 $staff_email,$option,$filter){
			//full query
			$sql = "SELECT staff_id,
							 staff_name,
							 staff_address,
							 staff_city,
							 staff_hp,
							 staff_email					  FROM 	 m_staff";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(staff_id) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(staff_name) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(staff_address) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(staff_city) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(staff_hp) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(staff_email) LIKE '%".addslashes(strtoloer($filter))."%' )";
			} else if($option=='SEARCH'){
				if($staff_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_id) LIKE '%".addslashes(strtolower($staff_id))."%'";
				};
				if($staff_name!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_name) LIKE '%".addslashes(strtolower($staff_name))."%'";
				};
				if($staff_address!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_address) LIKE '%".addslashes(strtolower($staff_address))."%'";
				};
				if($staff_city!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_city) LIKE '%".addslashes(strtolower($staff_city))."%'";
				};
				if($staff_hp!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_hp) LIKE '%".addslashes(strtolower($staff_hp))."%'";
				};
				if($staff_email!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_email) LIKE '%".addslashes(strtolower($staff_email))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function m_staff_export_excel($staff_id ,$staff_name ,$staff_address ,$staff_city ,$staff_hp ,$staff_email ,$option,$filter){
			//full query
			$sql = "SELECT staff_id,
							 staff_name,
							 staff_address,
							 staff_city,
							 staff_hp,
							 staff_email					  FROM 	 m_staff";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(staff_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(staff_name) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(staff_address) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(staff_city) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(staff_hp) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(staff_email) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($staff_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_id) LIKE '%".addslashes(strtolower($staff_id))."%'";
				};
				if($staff_name!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_name) LIKE '%".addslashes(strtolower($staff_name))."%'";
				};
				if($staff_address!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_address) LIKE '%".addslashes(strtolower($staff_address))."%'";
				};
				if($staff_city!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_city) LIKE '%".addslashes(strtolower($staff_city))."%'";
				};
				if($staff_hp!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_hp) LIKE '%".addslashes(strtolower($staff_hp))."%'";
				};
				if($staff_email!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(staff_email) LIKE '%".addslashes(strtolower($staff_email))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>